﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CollegeWebsite.Areas.Administrator.Models;

namespace CollegeWebsite.Areas.Administrator.Controllers
{
    public class DepartmentController : Controller
    {
        readonly CollegeWebsiteDataContext _dataContext = new CollegeWebsiteDataContext();

        // GET: /Department/
        public ActionResult Index()
        {
            List<Department> listDepartment = _dataContext.Departments.Select(d => d).ToList();
            return View(listDepartment);
        }

        public ActionResult ListByFaculty(int id)
        {
            List<Department> listDepartment = _dataContext.Departments.Select(d => d).Where(d=>d.FacultyID == id).ToList();
            if (listDepartment.Count < 1)
            {
                ViewData["ErrorInfo"] = "There are no content";
                return View("Error");
            }
            return View("Index",listDepartment);
        }


        // GET: /Department/Details/5
        public ActionResult Details(int id)
        {
            Department dep = _dataContext.Departments.Select(d => d).Single(d => (d.DepartmentID == id));
            // Department dep__2 = _dataContext.Departments.Select(d => d).Where(d => (d.DepartmentID == id)).Single();
            if (dep != null)
            {
                return View(dep);
            }
            else
            {
                return RedirectToAction("Index");
            }

        }

        //
        // GET: /Department/Create

        public ActionResult Create()
        {
            List<Faculty> faculty = _dataContext.Faculties.Select(f => f).Where(f => f.Status).ToList();
            ViewData["faculty"] = faculty;
            Department dep = new Department();
            
            dep.Status = true;
            
            return View(dep);
        }

        //
        // POST: /Department/Create

        [HttpPost, ValidateInput(false)]
        public ActionResult Create(Department dep)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    _dataContext.Departments.InsertOnSubmit(dep);
                    _dataContext.SubmitChanges();
                    return RedirectToAction("Index");
                }
                return View();
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Department/Edit/5

        public ActionResult Edit(int id)
        {
            List<Faculty> faculty = _dataContext.Faculties.Select(f => f).Where(f => f.Status).ToList();
            ViewData["faculty"] = faculty;
            Department department = _dataContext.Departments.Single(d => d.DepartmentID == id);
            
            return View(department);
        }

        //
        // POST: /Department/Edit/5

        [HttpPost, ValidateInput(false)]
        public ActionResult Edit(int id, Department newDept)
        {
            try
            {
                Department oldDept = _dataContext.Departments.Single(d => d.DepartmentID == id);
                oldDept.FacultyID = newDept.FacultyID;
                oldDept.DepartmentName = newDept.DepartmentName;
                oldDept.DepartmentDescription = newDept.DepartmentDescription;
                oldDept.Status = newDept.Status;

                _dataContext.SubmitChanges();


                return RedirectToAction("Index");
            }
            catch
            {
                return View(newDept);
            }
        }

        //
        // GET: /Department/Delete/5

        public ActionResult Delete(int id)
        {
            Department dep = _dataContext.Departments.Single(d => d.DepartmentID == id);

            return View(dep);
        }

        //
        // POST: /Department/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection formCollection)
        {
            try
            {
                Department department = _dataContext.Departments.Single(d => d.DepartmentID == id);
                _dataContext.Departments.DeleteOnSubmit(department);
                _dataContext.SubmitChanges();

                return RedirectToAction("Index");
            }
            catch (Exception exception)
            {
                ViewData["ErrorInfo"] = exception.Message;
                return View("Error");
            }
        }
    }
}
